<html>
<head><meta charset="utf-8"><title>Does compiling with rustc increase bin size vs not using it? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html">Does compiling with rustc increase bin size vs not using it?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211726054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211726054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Grommish <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211726054">(Sep 30 2020 at 07:52)</a>:</h4>
<p>I know I'm asking a terribly basic question, and I'm not looking for a <em>developer</em> technical answer, but more of a general question regarding the finished product. </p>
<p>Does incorporating rust into a program cause its binary to increase/decrease/the question doesn't apply the overall size of a finished binary vs a standard stock toolchain?</p>



<a name="211737469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211737469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211737469">(Sep 30 2020 at 09:54)</a>:</h4>
<p>AFAIK, there are two things going on, in that regard:</p>
<ul>
<li>Rust programs very often rely on the standard library, and although the linker can optimize away the unused parts, there are very often two things that still remain there: string formatting and the unwinding "runtime". This is what causes a simple <code>println!("Hello, World!");</code> program to already be, by default considerably bigger than its very tiny C counterpart. That being said, such addition is <em>fixed</em>, or at least upper-bounded by the size of the standard library itself, so for big projects that shouldn't matter (Also, see the <a href="https://github.com/johnthagen/min-sized-rust">https://github.com/johnthagen/min-sized-rust</a> repo for some examples as to how to minimize the binary size for the case of "small programs").</li>
<li>The second thing, and, imho, the one that has most impact on binary size, is how easy it is to bundle dependencies within Rust projects. A simple line added to a file, or a simple <code>cargo add dependency_name</code> command, and <em>voilà</em> ! This is one of the greatest things about using Rust, nowadays, that is not directly tied to the language but to the modern tooling around that ecosystem. And I do think that having this power is great, leading to higher quality software (compare that to the C / C++ world where people would often re-roll their own implementation if it avoided the hassle of pulling in another dependency). But it also comes with a drawback: Rust projects thus have a tendency to have an increased number of direct dependencies compared to C / C++ counter-parts, which, recursively / transitively, does lead to a dependency tree which is quite big. So <strong>if left untamed</strong>, it can definitely lead to bigger binaries. That being said, it can be kept in check, by being more selective about the dependencies that get pulled. See this other blog post about it: <a href="https://blog.kodewerx.org/2020/06/the-rust-compiler-isnt-slow-we-are.html">https://blog.kodewerx.org/2020/06/the-rust-compiler-isnt-slow-we-are.html</a> (they mention compilation <em>time</em> rather than <em>size</em>, but obviously they are kind of related, especially when talking about the number and size of the dependencies)</li>
</ul>



<a name="211780635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211780635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211780635">(Sep 30 2020 at 15:49)</a>:</h4>
<p>As usual for binary output questions, the answer is that it doesn't have all the much to do with _rust_ itself and much more to do with LLVM and what code you write, <span class="user-mention" data-user-id="332271">@Grommish</span>.  The binary size you get from rustc is about the same as you'd get from a C++ one with clang that was using similar levels of checking (like <code>std::vector::at</code> and not unsafe indexing) and similar sizes of libraries.</p>



<a name="211807644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211807644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Grommish <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211807644">(Sep 30 2020 at 19:21)</a>:</h4>
<p>I appreciate the responses!  I'm currently incorporating rust into the OpenWrt build environment, and the concern is always size when dealing with some of thier devices. </p>
<p>Again, then you for the info!</p>



<a name="211807766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211807766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211807766">(Sep 30 2020 at 19:22)</a>:</h4>
<p>Make sure you check out the general Rust embedded world as well. I've an Arduino Uno sitting on my desk with Rust code on it. Rust can be small.</p>



<a name="211864726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211864726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Grommish <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211864726">(Oct 01 2020 at 03:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F/near/211807766">said</a>:</p>
<blockquote>
<p>Make sure you check out the general Rust embedded world as well. I've an Arduino Uno sitting on my desk with Rust code on it. Rust can be small.</p>
</blockquote>
<p>Oh, interesting.  In my case, I was concerned about the overall size since almost everyone would be cross compiling to another arch, but that title makes it sound like rust compiler itself can be pushed. I will certainly look into it.  Thanks!</p>



<a name="211869267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Does%20compiling%20with%20rustc%20increase%20bin%20size%20vs%20not%20using%20it%3F/near/211869267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Does.20compiling.20with.20rustc.20increase.20bin.20size.20vs.20not.20using.20it.3F.html#211869267">(Oct 01 2020 at 04:51)</a>:</h4>
<p>worth checking out <a href="https://github.com/johnthagen/min-sized-rust">https://github.com/johnthagen/min-sized-rust</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>